package learn_project;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Vv
 * @version 1.0
 * @project coding_learn
 * @description
 * @date 2023/9/25 09:29:00
 */
public class List_IO {
    public static void main(String[] args) {

    }
    public static void listToFile(List<Student> list) throws IOException {
        File file = new File("d:/elm_v1/Student.txt");
        FileWriter fileWriter = null;
        BufferedWriter bw = null;
        try {
            fileWriter = new FileWriter(file,false);
            bw = new BufferedWriter(fileWriter);
            for(int i = 0;i<list.size();i++){
                Student stu = (Student) list.get(i);
                StringBuilder sbr = new StringBuilder();
                sbr.append(stu.getSno() + " ");
                sbr.append(stu.getSname()+" ");
                bw.write(sbr.toString());
                bw.newLine();
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            bw.close();
            fileWriter.close();
        }
    }
    public static List<Student> fileToList() throws IOException{
        List<Student> list = new ArrayList<>();
        File file = new File("d:/elm_v1/Student.txt");
        FileReader fr = new FileReader(file);
        BufferedReader br = new BufferedReader(fr);
        while(true){
            String str = br.readLine();
            if(str != null){
                System.out.println(str);
                String[] arr = str.split(" ");
                Student stu = new Student();
                stu.setSno(Integer.parseInt(arr[0]));
                stu.setSname(arr[1]);
                list.add(stu);
            }else{
                break;
            }
        }
        return list;
    }
}
